package com.veo.dao.Impl;

import com.veo.dao.UserDao;
import com.veo.entity.User;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

    @Resource(name = "sessionFactory")
    public void setSessionFactory1(SessionFactory sessionFactory){
        super.setSessionFactory(sessionFactory);
    }


    @Override
    public User findLoginUser(User loginUser) {
        List<User> list = (List<User>) this.getHibernateTemplate().find("from User where username='" + loginUser.getUsername() + "' and password='" + loginUser.getPassword()+"'");
        if (list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public int updateUserInfo(User user) {
        assert this.getHibernateTemplate() != null;
        this.getHibernateTemplate().update(user);
        return 1;
    }
}
